#coding=utf-8
import os
import sys
import openpyxl

from excel_util import set_style
from word_util import WordAttributes, Word
from common_util import find_first_CHINESE_CHAR

def script_path():
	import inspect, os
	caller_file = inspect.stack()[1][1]         # caller's filename
	return os.path.abspath(os.path.dirname(caller_file))# path
    
local = script_path()
path = os.path.join(local,"word_new.txt")

def read_all_words(word_dict):
    f = open(path, 'r+', encoding='utf-8')
    for i in f:
        i = i.replace('\n', '')
        i = i.strip()
        pos = find_first_CHINESE_CHAR(i)
        if pos < 0:
            continue
        word = Word(i)
        word.parse()
        if word.is_phrase():
            words[word.wordEx()] = word
        else:
            word_dict[word.word] = word
    f.close()

def get_all_type(word_dict, type_list):
    for i in word_dict.keys():
        word = word_dict[i]
        if word.att is None:
            continue
        for t in word.att.types:        
            if t in type_list:
                continue
            type_list.append(t)

words={}
types=[]

read_all_words(words)
get_all_type(words, types)

words = sorted(words.items(), key=lambda d: d[0].lower())
    
def add_head(sheet):
    sheet.cell(row=1, column=1, value="编号")
    sheet.cell(row=1, column=2, value="名称")
    sheet.cell(row=1, column=3, value="类型")
    sheet.cell(row=1, column=4, value="属性")
    sheet.cell(row=1, column=5, value="描述")

def add_row(sheet, row, word):
    cellIndex = 1
    sheet.cell(row=row, column=cellIndex+0, value=row - 1)
    sheet.cell(row=row, column=cellIndex+1, value=word.wordEx())
    sheet.cell(row=row, column=cellIndex+2, value=word.types_str())
    att = word.attributes
    att.replace("\t", "")
    if word.att is not None:
        for type in word.att.types:
            att = att.replace(type, "")
            
    if word.is_phrase():
        att = ""
    sheet.cell(row=row, column=cellIndex+3, value=att)
    sheet.cell(row=row, column=cellIndex+4, value=word.chinese)

path2 = os.path.join(local,"english_words.xlsx")
#wb = openpyxl.load_workbook(path2)
wb = openpyxl.Workbook()
# 修改当前工作表的名称
sheet = wb.active

def all_words_2_excel(sheet, word_dict):
    # 修改工作表的名称
    sheet.title = "1.allWords"
    add_head(sheet)
    row_index = 1
    for word2 in word_dict:
        word = word2[1]
        if len(word.word) < 1:
            continue
        row_index += 1
        add_row(sheet, row_index, word)
    set_style(sheet, row_index)
    return row_index

def add_phrase_sheet(wb, word_dict):
    sheet = wb.create_sheet("2.phrase")
    add_head(sheet)
    row_index = 1

    for word2 in word_dict:
        word = word2[1]
        if not word.is_phrase():
            continue
        row_index += 1
        add_row(sheet, row_index, word)
    set_style(sheet, row_index)
    return row_index
    
def _add_type_words_2_excel(word_dict, type, index):
    sheet = wb.create_sheet(str(index) + "." + type.replace(".", ""))
    add_head(sheet)
    row_index = 1
    for word2 in word_dict:
        word = word2[1]
        if word.att is None:
            continue
        if type not in word.att.types:
            continue
        row_index += 1
        add_row(sheet, row_index, word)
    set_style(sheet, row_index)
    return row_index
 
def add_all_tye_2_excel(word_dict, type_list):
    sheet_index = 2
    for i in type_list:
        print(i)
        sheet_index += 1
        row_count = _add_type_words_2_excel(word_dict, i, sheet_index)
        total[i] = row_count
    
def add_type_2_dict(type_dict):
    type_dict["total"]="总数"
    type_dict["phrase"]="短语"
    type_dict["adj."]="形容词"
    type_dict["adv."]="副词"
    type_dict["art."]="冠词"
    type_dict["aux."]="情态动词"
    type_dict["adj."]="形容词"
    type_dict["conj."]="连词"
    type_dict["interj."]="感叹词"
    type_dict["n."]="名词"

    type_dict["num."]="介词"
    type_dict["prep."]="介词"
    type_dict["pron."]="代词"
    type_dict["v."]="动词"
    type_dict["vi."]="不及物动词"
    type_dict["vt."]="及物动词"
 
def add_total_2_excel(wb, type_dict, total_dict):
    sheet = wb.create_sheet("total", index=0)
    sheet.cell(row=1, column=1, value="编号")
    sheet.cell(row=1, column=2, value="名称")
    sheet.cell(row=1, column=3, value="中文")
    sheet.cell(row=1, column=4, value="总数")
    row_index = 2
    for k, v in total_dict.items():
        sheet.cell(row=row_index, column=1, value=str(row_index - 1))
        sheet.cell(row=row_index, column=2, value=k)
        sheet.cell(row=row_index, column=3, value=type_dict.get(k))
        sheet.cell(row=row_index, column=4, value=str(v-1))
        row_index += 1
        
    set_style(sheet, row_index - 1)
    sheet.delete_cols(5,2)
    
total={}
row_count = all_words_2_excel(sheet, words)
total["total"] = row_count
row_count = add_phrase_sheet(wb, words)
total["phrase"] = row_count
add_all_tye_2_excel(words, types)

types={}
add_type_2_dict(types)
add_total_2_excel(wb, types, total)

wb.save(filename=path2)